package com.anolesoft.yr.mbus.pre.service.impl;

import com.anolesoft.yr.mbus.pre.dto.*;
import com.anolesoft.yr.mbus.pre.mapper.*;
import com.anolesoft.yr.mbus.pre.service.PreInquiryService;
import com.anolesoft.yr.mbus.pre.vo.ObjectQueryVO;
import com.anolesoft.yr.mbus.pre.vo.PreInquiryQueryVO;
import com.anolesoft.yr.mbus.pre.vo.PreOrderQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeoutException;

@Service
public class PreInquiryServiceImpl implements PreInquiryService {

    private static final String SUCCESS = "SUCCESS";
    private static final String EXCEPTION = "EXCEPTION";

    @Autowired
    PreInquiryMapper preInquiryMapper;
    @Autowired
    PreOrderMapper preOrderMapper;
    @Autowired
    PreOrderProdMapper preOrderProdMapper;
    @Autowired
    PreOrderPartMapper preOrderPartMapper;
    @Autowired
    PreOrderServiceMapper preOrderServiceMapper;
    @Autowired
    PreOrderServiceOnlyMapper preOrderServiceOnlyMapper;
    @Autowired
    PreOrderMarkMapper preOrderMarkMapper;
    @Autowired
    PrePromoteMapper prePromoteMapper;

    //新增询盘记录
    @Override
    @Transactional
    public String savePreInquiry(PreInquirySaveDTO preInquirySaveDTO) throws Exception {
        try {
            int count = 0;

            count = preInquiryMapper.queryPreInquiryByPromoteUid(preInquirySaveDTO.getPrePromoteUid());
            if (count > 0) {
                List<ObjectQueryVO> ObjectQueryVOList = preInquiryMapper.queryPreInquiryUid(preInquirySaveDTO.getPrePromoteUid());
                for (int i = 0; i < ObjectQueryVOList.size(); i++) {
                    ObjectQueryVO objectQueryVO = ObjectQueryVOList.get(i);
                    String preOrderUid = preOrderMapper.queryPreOrderUid(objectQueryVO.getObjuid());

                    preOrderMapper.deletePreOrder(preOrderUid);
                    preOrderProdMapper.deletePreOrderProd(preOrderUid);
                    preOrderPartMapper.deletePreOrderPart(preOrderUid);
                    preOrderMarkMapper.deletePreOrderMarkSaveDTO(preOrderUid);
                    preOrderServiceMapper.deletePreOrderService(preOrderUid);
                    preOrderServiceOnlyMapper.deletePreOrderServiceOnly(preOrderUid);
                }
            }

            String crmBaseType = preInquirySaveDTO.getCrmBaseType();
            if("Insert".equals(crmBaseType)){
                List crmBaseSaveDTOList = preInquirySaveDTO.getCrmBaseSaveDTO();
                for(int i=0; i<crmBaseSaveDTOList.size(); i++){
                    CrmBaseSaveDTO crmBaseSaveDTO = (CrmBaseSaveDTO) crmBaseSaveDTOList.get(i);
                    count = preInquiryMapper.saveCrmBase(crmBaseSaveDTO);
                }

                preInquiryMapper.updatePreInquiryCrmBase(preInquirySaveDTO.getPrePromoteUid());
                prePromoteMapper.updatePrePromoteCrmBase(preInquirySaveDTO.getPrePromoteUid());
            }else{
                List crmBaseUpdateDTOList = preInquirySaveDTO.getCrmBaseUpdateDTO();
                for(int i=0; i<crmBaseUpdateDTOList.size(); i++){
                    CrmBaseUpdateDTO crmBaseUpdateDTO = (CrmBaseUpdateDTO) crmBaseUpdateDTOList.get(i);
                    count = preInquiryMapper.updateCrmBase(crmBaseUpdateDTO);
                }

                preInquiryMapper.updatePreInquiryCrmBase(preInquirySaveDTO.getPrePromoteUid());
                prePromoteMapper.updatePrePromoteCrmBase(preInquirySaveDTO.getPrePromoteUid());
            }

            List preOrderSaveDTOList = preInquirySaveDTO.getPreOrderSaveDTO();
            for (int i = 0; i < preOrderSaveDTOList.size(); i++) {
                PreOrderSaveDTO preOrderSaveDTO = (PreOrderSaveDTO) preOrderSaveDTOList.get(i);

                List preOrderProdSaveDTOList = preOrderSaveDTO.getPreOrderProdSaveDTO();
                for (int j = 0; j < preOrderProdSaveDTOList.size(); j++) {
                    PreOrderProdSaveDTO preOrderProdSaveDTO = (PreOrderProdSaveDTO) preOrderProdSaveDTOList.get(j);

                    List preOrderPartSaveDTOList = preOrderProdSaveDTO.getPreOrderPartSaveDTO();
                    for (int k = 0; k < preOrderPartSaveDTOList.size(); k++) {
                        PreOrderPartSaveDTO preOrderPartSaveDTO = (PreOrderPartSaveDTO) preOrderPartSaveDTOList.get(k);

                        List preOrderServiceSaveDTOList = preOrderPartSaveDTO.getPreOrderServiceSaveDTO();
                        for (int l = 0; l < preOrderServiceSaveDTOList.size(); l++) {
                            PreOrderServiceSaveDTO preOrderServiceSaveDTO = (PreOrderServiceSaveDTO) preOrderServiceSaveDTOList.get(l);
                            count = preOrderServiceMapper.savePreOrderService(preOrderServiceSaveDTO);
                        }
                        count = preOrderPartMapper.savePreOrderPart(preOrderPartSaveDTO);
                    }

                    List preOrderMarkSaveDTOList = preOrderProdSaveDTO.getPreOrderMarkSaveDTO();
                    for (int k = 0; k < preOrderMarkSaveDTOList.size(); k++) {
                        PreOrderMarkSaveDTO preOrderMarkSaveDTO = (PreOrderMarkSaveDTO) preOrderMarkSaveDTOList.get(k);
                        count = preOrderMarkMapper.savePreOrderMarkSaveDTO(preOrderMarkSaveDTO);
                    }

                    List preOrderServiceOnlySaveDTOList = preOrderProdSaveDTO.getPreOrderServiceOnlySaveDTO();
                    for (int k = 0; k < preOrderServiceOnlySaveDTOList.size(); k++) {
                        PreOrderServiceOnlySaveDTO preOrderServiceOnlySaveDTO = (PreOrderServiceOnlySaveDTO) preOrderServiceOnlySaveDTOList.get(k);
                        count = preOrderServiceOnlyMapper.savePreOrderServiceOnly(preOrderServiceOnlySaveDTO);
                    }
                    count = preOrderProdMapper.savePreOrderProd(preOrderProdSaveDTO);
                }
                count = preOrderMapper.savePreOrder(preOrderSaveDTO);
            }
            count = preInquiryMapper.savePreInquiry(preInquirySaveDTO);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //查询配件信息（临时查询，正常应该在配件接口中）
    @Override
    public List<ObjectQueryVO> queryPartModelName() throws Exception {
        List<ObjectQueryVO> objectQueryVOList = preInquiryMapper.queryPartModelName();
        return objectQueryVOList;
    }

    //查询配件询价（临时查询，正常应该在配件询价接口中）
    @Override
    public List<ObjectQueryVO> querySupBasePart(String partModel) throws Exception {
        List<ObjectQueryVO> objectQueryVOList = preInquiryMapper.querySupBasePart(partModel);
        return objectQueryVOList;
    }

    //查询配件报价（临时查询，正常应该在配件报价接口中）
    @Override
    public List<ObjectQueryVO> queryCrmBasePartPrice(String partModel) throws Exception {
        List<ObjectQueryVO> objectQueryVOList = preInquiryMapper.queryCrmBasePartPrice(partModel);
        return objectQueryVOList;
    }

    //查询服务报价（临时查询，正常应该在服务报价接口中）
    @Override
    public List<ObjectQueryVO> queryCrmBaseServicePrice(String prodName) throws Exception {
        List<ObjectQueryVO> objectQueryVOList = preInquiryMapper.queryCrmBaseServicePrice(prodName);
        return objectQueryVOList;
    }

    //查询报价单信息（回访询盘、生成、驳回）
    @Override
    public List<PreOrderQueryVO> queryPreOrderAll(String preInquiryUid) throws Exception {
        List<PreOrderQueryVO> preOrderQueryVOList = preOrderMapper.queryPreOrderAll(preInquiryUid);
        return preOrderQueryVOList;
    }

    //根据主键查询询盘记录，用于询盘回访界面的显示（推广主键查询）
    @Override
    public List<PreInquiryQueryVO> queryPreInquiryUid2(String prePromoteUid) throws Exception {
        List<PreInquiryQueryVO> preInquiryQueryVOList = preInquiryMapper.queryPreInquiryUid2(prePromoteUid);
        return preInquiryQueryVOList;
    }

    //根据主键查询询盘记录，用于询盘回访界面的显示（拜访主键查询）
    @Override
    public List<PreInquiryQueryVO> queryPreInquiryUid3(String vistUid) throws Exception {
        List<PreInquiryQueryVO> preInquiryQueryVOList = preInquiryMapper.queryPreInquiryUid3(vistUid);
        return preInquiryQueryVOList;
    }

    //根据主键查询询盘记录，用于询盘回访界面的显示（询盘主键查询）
    @Override
    public List<PreInquiryQueryVO> queryPreInquiryUid4(String preInquiryUid) throws Exception {
        List<PreInquiryQueryVO> preInquiryQueryVOList = preInquiryMapper.queryPreInquiryUid4(preInquiryUid);
        return preInquiryQueryVOList;
    }

}
